package cn.tao.org.service.impl;


import cn.tao.base.service.impl.BaseServiceImpl;
import cn.tao.org.domain.Employee;
import cn.tao.org.domain.Shop;
import cn.tao.org.mapper.EmployeeMapper;
import cn.tao.org.mapper.ShopMapper;
import cn.tao.org.service.IShopService;
import cn.tao.user.domain.LoginInfo;
import cn.tao.user.mapper.LoginInfoMapper;
import cn.tao.utils.MD5Utils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.UUID;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    //springboot很email集成后管理的核心对象，邮件的核心对象
    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private LoginInfoMapper loginInfoMapper;


    /**
     * 批量删除，接收一个long类型的数组
     * @param ids
     */
    @Override
    @Transactional
    public void batchDelete(Long[] ids) {

        shopMapper.batchDelete(ids);
    }

    /**
     * 通过修改shop的状态
     * @param shop
     */
    @Override
    public void update(Shop shop){
        shop.setState(1);
        shopMapper.update(shop);
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();

            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"UTF-8");

            //设置发件人
            helper.setFrom("157015146@qq.com");
            //设置收件人
            helper.setTo("157015146@qq.com");
            //设置主题
            helper.setSubject("请点击链接，激活账号");
            Long eid = shop.getAdmin_id();
            helper.setText("<h1>账号激活</h1>\n" +
                    "    <a href=\"http://127.0.0.1:8081/#/activation?id="+eid+"\">确认激活</a>",true);

            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 商户入驻
     * @param shop
     */
    @Override
    public void settledIn(Shop shop) {
        shopMapper.save(shop);
        //先拿出admin对象，保存返回employee的id
        Employee admin = shop.getAdmin();
        //通过uuid拿到一组随机数
        String salt = UUID.randomUUID().toString();
        //使用MD5加密 密码+盐 一起加密
        String md5 = MD5Utils.encrypByMd5(admin.getPassword() + salt);
        admin.setSalt(salt);
        admin.setPassword(md5);
        // 0待激活 1启用 -1 封号
        admin.setState(1);
        employeeMapper.save(admin);

        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setDisable(admin.getState());
        //0后台用户
        loginInfo.setType(0);
        BeanUtils.copyProperties(admin,loginInfo);
        loginInfoMapper.save(loginInfo);
        admin.setShop_id(shop.getId());
        admin.setLogininfo_id(loginInfo.getId());
        employeeMapper.update(admin);
    }
}
